package protocol.work.connection.http;

import protocol.common.entity.ConnectionWorkState;
import protocol.common.entity.HttpConnConfig;
import protocol.common.httpclient.HttpRequestManager;
import protocol.work.connection.AbstractConnection;
import redis.clients.jedis.Jedis;

public class HttpWorkConnection extends AbstractConnection{
	private IConnectionWork  connectionWork;
	
	private HttpConnConfig httpConnConfig;
	
	public HttpWorkConnection(HttpConnConfig httpConnConfig,IConnectionWork connectionWork){
		super(httpConnConfig.getChannle());
		this.connectionWork = connectionWork;
		this.httpConnConfig = httpConnConfig;
	}
	
	public void run(){
		HttpRequestManager httpRequestManager = HttpRequestManager.pullManagerFromMap(httpConnConfig.getChannle());
		while(super.initMark){
			Jedis jedis = this.getJedis();
			try{
				ConnectionWorkState workState = connectionWork.handle(jedis,httpRequestManager);
				switch (workState) {
					case END:
						super.stopThread();
						break;
					case FREE:
						super.threadSleep(5000L);
						break;
					case SUCCESS:
						continue;
				}
			} catch (Exception e) {
				super.exceptionHandle(e);
			} finally{
				this.returnJedis(jedis);
			}
		}
	}
}
